home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************************
- *
- *
- * MacZoop - "the framework for the rest of us"
- *
- *
- *
- * ZPictWindow.cpp -- a window that displays PICT files
- *
- *
- *
- *
- *
- * © 1996, Graham Cox
- *
- *
- *
- *
- *************************************************************************************************/
-
-
- #include "ZPictWindow.h"
- #include "MacZoop.h"
- #include "ZFile.h"
-
- #include <ImageCompression.h>
-
- CLASSCONSTRUCTOR( ZPictWindow );
-
- /*--------------------------------*** CONSTRUCTOR ***---------------------------------*/
-
- ZPictWindow::ZPictWindow( ZCommander* aBoss, const short windID )
- : ZScroller( aBoss, windID, TRUE, TRUE )
- {
- classID = CLASS_ZPictWindow;
-
- thePicture = NULL;
- printable = TRUE;
-
- savePreview = FALSE;
- saveCustomIcon = FALSE;
- }
-
-
- ZPictWindow::ZPictWindow()
- : ZScroller()
- {
- classID = CLASS_ZPictWindow;
-
- thePicture = NULL;
- printable = TRUE;
-
- savePreview = FALSE;
- saveCustomIcon = FALSE;
- }
-
-
- /*---------------------------------*** DESTRUCTOR ***---------------------------------*/
-
- ZPictWindow::~ZPictWindow()
- {
- if (thePicture)
- KillPicture(thePicture);
- }
-
- /*--------------------------------*** DRAWCONTENT ***---------------------------------*/
- /*
-
- overrides ZScroller to draw the picture in the window
-
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::DrawContent()
- {
- Rect pFrame;
-
- if (thePicture)
- {
- pFrame = (*thePicture)->picFrame;
- OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
- DrawPicture(thePicture, &pFrame);
- }
- }
-
-
- /*----------------------------------*** OPENFILE ***----------------------------------*/
- /*
-
- overrides the base ZWindow to open a PICT file on disk to the window
-
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::OpenFile( const OSType fType, Boolean isStationery )
- {
- // read the picture into the picture handle
-
- FInfo fi;
- short refNum;
- long pSize;
- Handle temp = NULL;
- Rect pFrame;
-
- if (macFile.vRefNum != kNoFile)
- {
- FailOSErr(FSpGetFInfo(&macFile, &fi));
-
- if (fi.fdType != 'PICT')
- FailOSErr(paramErr);
-
- FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum ));
-
- // skip the first 512 bytes of the file
-
- try
- {
- FailOSErr(SetFPos(refNum, fsFromStart, 512));
-
- // how big is the rest of the file?
-
- FailOSErr(GetEOF(refNum, &pSize));
- pSize -= 512;
-
- if (pSize > 0)
- {
- FailNIL(temp = NewHandle(pSize));
-
- HLock(temp);
- FailOSErr(FSRead(refNum, &pSize, *temp));
- HUnlock(temp);
- }
-
- FSClose(refNum);
-
- if (thePicture)
- KillPicture(thePicture);
-
- thePicture = (PicHandle)temp;
-
- // set the scroll dimensions to the picture's frame
-
- pFrame = (*thePicture)->picFrame;
- OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
- SetBounds( pFrame );
- SetScrollAmount(8,8);
-
- pFrame.right += kStdScrollbarWidth;
- pFrame.bottom += kStdScrollbarWidth;
-
- SetSize(pFrame.right, pFrame.bottom);
- }
- catch(OSErr err)
- {
- FSClose(refNum);
-
- if (temp)
- {
- HUnlock(temp);
- DisposeHandle(temp);
- }
- throw err;
- }
- }
- ZScroller::OpenFile( fType, isStationery );
- }
-
-
-
- /*--------------------------------*** SAVEFILE ***---------------------------------*/
- /*
-
- overrides the base ZWindow to save the image as a PICT file on disk
-
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::SaveFile()
- {
- // this saves the window contents as a PICT file in the file <macFile>.
-
- Handle header = NULL;
- ZFile* theFile;
-
- if ((macFile.vRefNum != kNoFile) && (thePicture != NULL))
- {
- FailNIL( theFile = new ZFile( macFile ));
- theFile->SetType( 'PICT' );
-
- if (! theFile->IsReal())
- theFile->Create();
-
- // open the file for safe-saving
-
- theFile->OpenSafe();
-
- try
- {
- // make a header and write it out. This is just 512 bytes of zeroes.
-
- FailNIL( header = NewHandleClear( 512L ));
-
- theFile->Write( header );
- DisposeHandle(header);
- header = NULL;
-
- // now write the picture handle
-
- theFile->Write((Handle) thePicture );
-
- // if the image comp mgr and QTime is available, make a preview and save it
-
- if ( gMacInfo.hasImgCompressionMgr &&
- gMacInfo.hasQuickTime &&
- savePreview )
- {
- PicHandle preview;
- short ref;
-
- FailNIL( preview = (PicHandle) NewHandle( 0 ));
- FailOSErr( MakeThumbnailFromPicture( thePicture, 8, preview, NULL ));
-
- theFile->CreateResFork();
- theFile->OpenResFork();
-
- ref = theFile->GetResourceRefNumber();
-
- try
- {
- FailOSErr( AddFilePreview( ref,'PICT', (Handle) preview ));
- }
- catch( OSErr err )
- {
- theFile->CloseResFork();
- throw err;
- }
-
- theFile->CloseResFork();
- }
-
- if ( saveCustomIcon )
- theFile->MakeCustomIcon( thePicture );
-
- theFile->Close();
- macFType = 'PICT';
- }
- catch( OSErr err )
- {
- theFile->Close();
- ForgetObject( theFile );
-
- if (header)
- DisposeHandle(header);
-
- throw err;
- }
-
- ForgetObject( theFile );
- ZScroller::SaveFile();
- }
- }
-
- /*--------------------------------*** UPDATEMENUS ***---------------------------------*/
- /*
- Enable the Copy command if we have a picture
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::UpdateMenus()
- {
- if ( thePicture )
- gMenuBar->EnableCommand( kCmdCopy );
-
- ZScroller::UpdateMenus();
- }
-
-
- /*--------------------------------*** CANPASTETYPE ***--------------------------------*/
- /*
- we can accept Paste commands of type PICT
- ----------------------------------------------------------------------------------------*/
-
- Boolean ZPictWindow::CanPasteType()
- {
- return gClipboard->QueryType( 'PICT' );
- }
-
-
- /*-----------------------------------*** DOCOPY ***-----------------------------------*/
- /*
- copy the image to the clipboard
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::DoCopy()
- {
- if ( thePicture )
- gClipboard->PutData( thePicture );
- }
-
- /*----------------------------------*** DOPASTE ***-----------------------------------*/
- /*
- copy a PICT from the clipboard to our local picture
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::DoPaste()
- {
- Rect pFrame;
- PicHandle p = (PicHandle) gClipboard->GetData( 'PICT' );
-
- FailNIL( p );
-
- if ( thePicture )
- KillPicture( thePicture );
-
- thePicture = p;
-
- pFrame = (*thePicture)->picFrame;
- OffsetRect( &pFrame, -pFrame.left, -pFrame.top );
- SetBounds( pFrame );
- SetScrollAmount( 8, 8 );
-
- pFrame.right += kStdScrollbarWidth;
- pFrame.bottom += kStdScrollbarWidth;
-
- SetSize( pFrame.right, pFrame.bottom );
-
- dirty = TRUE;
- }
-
-
- /*--------------------------*** SETPICTUREFROMRESOURCE ***----------------------------*/
- /*
- set the picture from a current resource file
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::SetPictureFromResource( short pictResID )
- {
- PicHandle p = GetPicture( pictResID );
-
- FailNILRes( p );
-
- KillPicture( thePicture );
- thePicture = p;
-
- Rect pFrame = (*thePicture)->picFrame;
- OffsetRect( &pFrame, -pFrame.left, -pFrame.top );
- SetBounds( pFrame );
-
- dirty = TRUE;
- }
-
-
- /*-------------------------------*** WRITETOSTREAM ***--------------------------------*/
- /*
- write window state and picture itself to the stream
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::WriteToStream( ZStream* aStream )
- {
- #if _MACZOOP_STREAMS
- ZScroller::WriteToStream( aStream );
-
- aStream->WriteChar( savePreview );
- aStream->WriteChar( saveCustomIcon );
- aStream->WriteHandle((Handle) thePicture );
- #endif
- }
-
-
- /*------------------------------*** READFROMSTREAM ***--------------------------------*/
- /*
- read window state and picture itself from the stream
- ----------------------------------------------------------------------------------------*/
-
- void ZPictWindow::ReadFromStream( ZStream* aStream )
- {
- #if _MACZOOP_STREAMS
- ZScroller::ReadFromStream( aStream );
-
- aStream->ReadChar((char*) &savePreview );
- aStream->ReadChar((char*) &saveCustomIcon );
- aStream->ReadHandle((Handle*) &thePicture );
- #endif
- }
-
-